package com.intellagric.service.search.dao;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.service.search.mapper.IftDataRecordSearchMapper;
import com.intellagric.service.search.pojo.IftDataRecord;
import com.intellagric.service.search.pojo.IftDataRecord2;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/12/31
 * @DESC： 物联网数据记录结果搜索
 */
@Repository
public class IftDataRecordSearchDao {

    @Autowired
    private SolrServer solrServer;

    /**
     * 查询采集数据记录
     * @param query
     * @return LayuiDataGridResult
     */
    public LayuiDataGridResult search(SolrQuery query) throws Exception {
        //根据query查询索引库
        QueryResponse queryResponse = solrServer.query(query);
        //取查询结果
        SolrDocumentList solrDocumentList = queryResponse.getResults();
        //获取总记录数量
        long numFound = solrDocumentList.getNumFound();
        //采集数据列表
        List<IftDataRecord> dataRecordList = new ArrayList<>();

        for (SolrDocument solrDocument : solrDocumentList) {
            IftDataRecord dataRecord = new IftDataRecord();
            dataRecord.setId((String) solrDocument.get("id"));
            dataRecord.setEquipmentId((String) solrDocument.get("equipment_id"));
            dataRecord.setSceneId((String) solrDocument.get("scene_id"));
            dataRecord.setEquipmentName((String) solrDocument.get("equipment_name"));
            dataRecord.setChannelNo((String) solrDocument.get("channel_NO"));
            dataRecord.setCheckItemId((String) solrDocument.get("check_item_id"));
            dataRecord.setCheckItemName((String) solrDocument.get("check_item_name"));
            dataRecord.setUnit((String) solrDocument.get("unit"));
            dataRecord.setProcessedData((String) solrDocument.get("processed_data"));
            dataRecord.setAcquisitionTime((Date) solrDocument.get("acquisition_time"));
            dataRecord.setReportingTime((Date) solrDocument.get("reporting_time"));
            dataRecord.setIsWarning((Integer) solrDocument.get("is_warning"));
            dataRecord.setIsRead((Integer) solrDocument.get("is_read"));
            dataRecord.setIsTreatment((Integer) solrDocument.get("is_treatment"));
            dataRecord.setWarningReason((String) solrDocument.get("warning_reason"));
            //添加数据
            dataRecordList.add(dataRecord);
        }

        //反射方式获取
        /*long one = System.currentTimeMillis();
        *//*List<IftDataRecord> dataRecordList = queryResponse.getBeans(IftDataRecord.class);*//*
        List<IftDataRecord> dataRecordList = queryResponse.getBeans(IftDataRecord.class);
        long second = System.currentTimeMillis();
        System.out.println(second - one );*/
        return LayuiDataGridResult.success().add(dataRecordList,numFound);
    }

}
